import { Menu } from "src/views/menus/entities/menu.entity";
import { User } from "src/views/users/entities/user.entity";
import { Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from "typeorm";

@Entity('roles')
export class Role {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  code: string;

  @Column()
  roleName: string;

  @Column()
  remark: string;

  @Column()
  updateTime: Date;

  @ManyToMany(type => User, user => user.roles)
  accounts: User[]

  @ManyToMany(type => Menu, menu => menu.role, {
    cascade: true,
    onUpdate: 'CASCADE',
    onDelete: 'CASCADE'
  })
  @JoinTable({
    name: "role_menus",
    joinColumn: {
      name: "role_id",
      referencedColumnName: "id"
    },
    inverseJoinColumn: {
      name: "menu_id",
      referencedColumnName: "id"
    }
  })
  menus: Menu[]
}
